/**
 * 2018/10/31 星期三 10:48:53 Lynn Li created.
 */

package com.diver.flinkdemo.service.impl;


import com.diver.flinkdemo.entity.xxt.LocationInfo;
import com.diver.flinkdemo.entity.xxt.SosInfo;
import com.diver.flinkdemo.mapper.dorisMapper.LastLocationMapper;
import com.diver.flinkdemo.mapper.dorisMapper.SosInfoMapper;
import com.diver.flinkdemo.service.ISosInfoService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * 异常-SOS Service 实现
 * Created by Lynn Li on 2018/10/31.
 */
@Slf4j
public class SosInfoService implements ISosInfoService {

	SosInfoMapper mapper;
	SqlSession session = null;

	@Override
	public void saveSosInfoFromLocationInfo(LocationInfo localtionInfo) {
		try {
			session = DynamicDataSourceUtil.dorisSessionFactory.openSession(true);
			mapper = session.getMapper(SosInfoMapper.class);
			SosInfo sosInfo = new SosInfo();
			sosInfo.setMsgId(localtionInfo.getMsgId());
			sosInfo.setCardNum(localtionInfo.getImei());
			sosInfo.setLat(localtionInfo.getLat());
			sosInfo.setLon(localtionInfo.getLon());
			sosInfo.setExceptionMsg(localtionInfo.getExceptionMsg());
			sosInfo.setUploadTime(localtionInfo.getUploadTime());
			sosInfo.setCreateTime(localtionInfo.getCreateTime());
			mapper.insert(sosInfo);
		} catch (Exception e){
			log.error("eee:{}", e.getMessage());
		} finally {
			endOfSession(session);
		}
	}

	public void endOfSession(SqlSession session) {
		//关闭sqlSession
		if (session != null) {
			log.info("session commit -> session close");
//            session.commit();
			session.close();

		}
	}
}
